﻿/******************************************************************
** 文 件 名: Bu_DocReceive_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 1:28:04
** 描    述: 资料接收表编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.Collections.Generic;
using System.Data.Common;


namespace Honestysoft.Financing.WebUI.Module.Audit
{
	public partial class Bu_DocReceive_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }

        protected string _ProjectName = "";
        protected string _ProjectCode = "";
        protected string _Owner = "";
        protected string _Contractor = "";
        protected string _ContractName = "";

        protected string _ProjectID = "";
        protected string _ContractID = "";

        private DataTable dtYesNo = null;
		
		private void BindControl()
		{
            btnExit.Attributes.Add("onclick", "window.returnValue="+(Request.QueryString["IsNew"] == "1"?"true":"false")+";window.close();");

            dtYesNo = new DataTable();
            dtYesNo.Columns.Add("Text", typeof(string));
            dtYesNo.Columns.Add("Value", typeof(string));
            DataRow dr = dtYesNo.NewRow();
            dr["Text"] = "有  ";
            dr["Value"] = "1";
            dtYesNo.Rows.Add(dr);
            dr = dtYesNo.NewRow();
            dr["Text"] = "无";
            dr["Value"] = "0";
            dtYesNo.Rows.Add(dr);

            BindRadioList(rblContDoc1);
            BindRadioList(rblContDoc2);
            BindRadioList(rblContDoc3);
            BindRadioList(rblContDoc4);
            BindRadioList(rblContDoc5);
            BindRadioList(rblContDoc6);
            BindRadioList(rblContDoc7);
            BindRadioList(rblContDoc8);
            BindRadioList(rblContDoc9);
            BindRadioList(rblCD_One);

            BindRadioList(rblProjDoc1);
            BindRadioList(rblProjDoc2);
            BindRadioList(rblProjDoc3);
            BindRadioList(rblProjDoc4);
            BindRadioList(rblProjDoc5);
            BindRadioList(rblProjDoc6);
            BindRadioList(rblProjDoc7);
            BindRadioList(rblProjDoc8);
            BindRadioList(rblProjDoc9);
            BindRadioList(rblProjDoc10);
            BindRadioList(rblPD_One);

            BindRadioList(rblZJDWDoc1);
            BindRadioList(rblZJDWDoc2);
            BindRadioList(rblZJDWDoc3);
            BindRadioList(rblZJDWDoc4);
            BindRadioList(rblZD_One);
			//ControlHelper.BindEnum(ddlDocRecieveStatus, "DocRecieveStatus");//此处最好改成typeof(EnumItem.XXX)
		}

        private void BindRadioList(RadioButtonList rbl)
        {
            rbl.DataSource = dtYesNo;
            rbl.DataTextField = "Text";
            rbl.DataValueField = "Value";
            rbl.DataBind();
            rbl.SelectedIndex = 0;
        }

        private void Bind()
        {
            Honestysoft.Financing.BusinessLogic.Contract bll = new BusinessLogic.Contract(Globals.CurrentConnStringName);
            BD_Contract contract = null;
			Bu_DocReceive obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Bu_DocReceive>(Globals.CurrentConnStringName).GetEntity(Request.QueryString["ID"]);               

                contract = bll.GetContract(obj.ContractID);
                hidContractID.Value = obj.ContractID;
                txtCode.Text = obj.Code;
                numAmount.Text = obj.Amount.ToString("0.00");
                txtOther.Text = obj.Other;
                hidFromCompany.Value = obj.FromCompany;
                txtFromEmployee.Text = obj.FromEmployee;
                hidToCompany.Value = obj.ToCompany;
                hidToEmployee.Value = obj.ToEmployee;
                txtToEmployeeName.Text = obj.ToEmployeeName;
                string[] arrCompID=new string[]{obj.FromCompany,obj.ToCompany};
                DataTable dtComp = new Honestysoft.Financing.BusinessLogic.Reports(Globals.CurrentConnStringName).GetCompanyName(arrCompID);
                DataRow[] rows = null;
                rows = dtComp.Select("ID='" + obj.FromCompany + "'");
                if (rows != null && rows.Length == 1)
                {
                    txtFromCompany.Text = rows[0]["CompName"].ToString();
                }
                rows = dtComp.Select("ID='" + obj.ToCompany + "'");
                if (rows != null && rows.Length == 1)
                {
                    txtToCompany.Text = rows[0]["CompName"].ToString();
                }
                dtpReceiveDate.Text = obj.ReceiveDate.ToString("yyyy-MM-dd");
                hidCreateTime.Value = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                hidCreator.Value = obj.Creator;
                txtFromEmployeePhone.Text = obj.FromEmployeePhone;

                rblContDoc1.SelectedValue = obj.IsCD1 ? "1" : "0";
                rblContDoc2.SelectedValue = obj.IsCD2 ? "1" : "0";
                rblContDoc3.SelectedValue = obj.IsCD3 ? "1" : "0";
                rblContDoc4.SelectedValue = obj.IsCD4 ? "1" : "0";
                rblContDoc5.SelectedValue = obj.IsCD5 ? "1" : "0";
                rblContDoc6.SelectedValue = obj.IsCD6 ? "1" : "0";
                rblContDoc7.SelectedValue = obj.IsCD7 ? "1" : "0";
                rblContDoc8.SelectedValue = obj.IsCD8 ? "1" : "0";
                rblContDoc9.SelectedValue = obj.IsCD9 ? "1" : "0";

                rblCD_One.SelectedValue = obj.IsCD_One ? "1" : "0";

                rblProjDoc1.SelectedValue = obj.IsPD1 ? "1" : "0";
                rblProjDoc2.SelectedValue = obj.IsPD2 ? "1" : "0";
                rblProjDoc3.SelectedValue = obj.IsPD3 ? "1" : "0";
                rblProjDoc4.SelectedValue = obj.IsPD4 ? "1" : "0";
                rblProjDoc5.SelectedValue = obj.IsPD5 ? "1" : "0";
                rblProjDoc6.SelectedValue = obj.IsPD6 ? "1" : "0";
                rblProjDoc7.SelectedValue = obj.IsPD7 ? "1" : "0";
                rblProjDoc8.SelectedValue = obj.IsPD8 ? "1" : "0";
                rblProjDoc9.SelectedValue = obj.IsPD9 ? "1" : "0";
                rblProjDoc10.SelectedValue = obj.IsPD10 ? "1" : "0";
                rblPD_One.SelectedValue = obj.IsPD_One ? "1" : "0";

                rblZJDWDoc1.SelectedValue = obj.IsZD1 ? "1" : "0";
                rblZJDWDoc2.SelectedValue = obj.IsZD2 ? "1" : "0";
                rblZJDWDoc3.SelectedValue = obj.IsZD3 ? "1" : "0";
                rblZJDWDoc4.SelectedValue = obj.IsZD4 ? "1" : "0";
                rblZD_One.SelectedValue = obj.IsZD_One ? "1" : "0";

                txtContDoc1.Text = obj.CD1;
                txtContDoc2.Text = obj.CD2;
                txtContDoc3.Text = obj.CD3;
                txtContDoc4.Text = obj.CD4;
                txtContDoc5.Text = obj.CD5;
                txtContDoc6.Text = obj.CD6;
                txtContDoc7.Text = obj.CD7;
                txtContDoc8.Text = obj.CD8;
                txtContDoc9.Text = obj.CD9;

                txtCD_One.Text = obj.CD_One;

                txtProjDoc1.Text = obj.PD1;
                txtProjDoc2.Text = obj.PD2;
                txtProjDoc3.Text = obj.PD3;
                txtProjDoc4.Text = obj.PD4;
                txtProjDoc5.Text = obj.PD5;
                txtProjDoc6.Text = obj.PD6;
                txtProjDoc7.Text = obj.PD7;
                txtProjDoc8.Text = obj.PD8;
                txtProjDoc9.Text = obj.PD9;
                txtProjDoc10.Text = obj.PD10;
                txtPD_One.Text = obj.PD_One;

                txtZJDWDoc1.Text = obj.ZD1;
                txtZJDWDoc2.Text = obj.ZD2;
                txtZJDWDoc3.Text = obj.ZD3;
                txtZJDWDoc4.Text = obj.ZD4;
                txtZD_One.Text = obj.ZD_One;
            }
            else
            {
                contract = bll.GetContract(Request.QueryString["ContractID"]);
             
                hidContractID.Value = Request.QueryString["ContractID"];
                dtpReceiveDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                txtFromCompany.Text = contract.OwnerName;// contract.ContractorName;
                hidFromCompany.Value = contract.Owner;// contract.Contractor;

                txtToCompany.Text = ConfigurationManager.AppSettings["YZName"];
                hidToCompany.Value = ConfigurationManager.AppSettings["YZID"];

                txtToEmployeeName.Text = UserLoginer.LoginUser.Name;
                hidToEmployee.Value = UserLoginer.LoginUser.ID;

                hidCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                hidCreator.Value = UserLoginer.LoginUser.ID;

                txtCode.Text = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetNextDocReceiveCode();

                //自动取得该项目最新作业表的项目文档是否属性
                DataTable dt = new Honestysoft.Financing.BusinessLogic.Project(Globals.CurrentConnStringName).GetProjectDocumentStatusByID(contract.ProjectID);
                if (dt != null && dt.Rows.Count == 1)
                {
                    rblProjDoc1.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD1"]) ? "1" : "0";
                    rblProjDoc2.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD2"]) ? "1" : "0";
                    rblProjDoc3.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD3"]) ? "1" : "0";
                    rblProjDoc4.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD4"]) ? "1" : "0";
                    rblProjDoc5.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD5"]) ? "1" : "0";
                    rblProjDoc6.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD6"]) ? "1" : "0";
                    rblProjDoc7.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD7"]) ? "1" : "0";
                    rblProjDoc8.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD8"]) ? "1" : "0";
                    rblProjDoc9.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD9"]) ? "1" : "0";
                    rblProjDoc10.SelectedValue = Convert.ToBoolean(dt.Rows[0]["IsPD10"]) ? "1" : "0";
                }
            }
            _ProjectName = contract.ProjectName;
            _Owner = contract.OwnerName;
            _ContractName = contract.Name;
            _Contractor = contract.ContractorName;
            _ProjectID = contract.ProjectID;
            _ContractID = contract.ID;
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }


        private Bu_DocReceive Save()
        {
            Bu_DocReceive obj = GetSubmitObject();
            BaseBusinessLogic<Bu_DocReceive> bll = new BaseBusinessLogic<Bu_DocReceive>(Globals.CurrentConnStringName);
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                bll.UpdateEntity(obj);
            }
            else
            {
                bll.CreateEntity(obj);
            }
            return obj;
        }
        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            try
            {
                Bu_DocReceive obj = Save();
                //ScriptHelper.Window.Alert("保存成功！", "window.location.href='DocReceivePrint.aspx?ID=" + obj.ID + "&FunctionID=" + FunctionID + "&IsUse=0&IsNew=1';");
                Response.Redirect("DocReceivePrint.aspx?ID=" + obj.ID + "&FunctionID=" + FunctionID + "&IsUse=0&IsNew=1");
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {
            try
            {
                Bu_DocReceive obj = Save();
                Response.Redirect("DocReceivePrint.aspx?ID=" + obj.ID + "&FunctionID=" + FunctionID + "&IsUse=0&IsNew=" + Request.QueryString["IsNew"]);

               // ScriptHelper.Window.Alert("保存成功！", "window.location.href='';");
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Bu_DocReceive GetSubmitObject()
        {
            Bu_DocReceive obj = new Bu_DocReceive();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
				obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            //obj.ID = txtID.Text;
			obj.Code = txtCode.Text;
            obj.ContractID = hidContractID.Value;// txtContractID.Text;
			if(numAmount.Text.Trim().Length > 0){obj.Amount = Convert.ToDecimal(numAmount.Text);}
			obj.Other = txtOther.Text;
            obj.FromCompany = hidFromCompany.Value;// txtFromCompany.Text;
			obj.FromEmployee = txtFromEmployee.Text;
            obj.ToCompany = hidToCompany.Value;// txtToCompany.Text;
            obj.ToEmployee = hidToEmployee.Value;// txtToEmployee.Text;
			obj.ToEmployeeName = txtToEmployeeName.Text;
			if(dtpReceiveDate.Text.Trim().Length > 0){obj.ReceiveDate = Convert.ToDateTime(dtpReceiveDate.Text);}
			//if(dtpCreateTime.Text.Trim().Length > 0){obj.CreateTime = Convert.ToDateTime(dtpCreateTime.Text);}
            //obj.Creator = txtCreator.Text;
            //obj.DocRecieveStatus = ddlDocRecieveStatus.SelectedValue;

            obj.CreateTime = Convert.ToDateTime(hidCreateTime.Value);
            obj.Creator = hidCreator.Value;

            obj.FromEmployeePhone = txtFromEmployeePhone.Text;

            obj.DocRecieveStatus = ((int)EnumItem.DocRecieveStatus.Accept).ToString();

            obj.IsCD1 = rblContDoc1.SelectedValue == "1";
            obj.IsCD2 = rblContDoc2.SelectedValue == "1";
            obj.IsCD3 = rblContDoc3.SelectedValue == "1";
            obj.IsCD4 = rblContDoc4.SelectedValue == "1";
            obj.IsCD5 = rblContDoc5.SelectedValue == "1";
            obj.IsCD6 = rblContDoc6.SelectedValue == "1";
            obj.IsCD7 = rblContDoc7.SelectedValue == "1";
            obj.IsCD8 = rblContDoc8.SelectedValue == "1";
            obj.IsCD9 = rblContDoc9.SelectedValue == "1";
            obj.IsCD_One = rblZD_One.SelectedValue == "1";

            obj.IsPD1 = rblProjDoc1.SelectedValue == "1";
            obj.IsPD2 = rblProjDoc2.SelectedValue == "1";
            obj.IsPD3 = rblProjDoc3.SelectedValue == "1";
            obj.IsPD4 = rblProjDoc4.SelectedValue == "1";
            obj.IsPD5 = rblProjDoc5.SelectedValue == "1";
            obj.IsPD6 = rblProjDoc6.SelectedValue == "1";
            obj.IsPD7 = rblProjDoc7.SelectedValue == "1";
            obj.IsPD8 = rblProjDoc8.SelectedValue == "1";
            obj.IsPD9 = rblProjDoc9.SelectedValue == "1";
            obj.IsPD10 = rblProjDoc10.SelectedValue == "1";
            obj.IsPD_One = rblPD_One.SelectedValue == "1";

            obj.IsZD1 = rblZJDWDoc1.SelectedValue == "1";
            obj.IsZD2 = rblZJDWDoc2.SelectedValue == "1";
            obj.IsZD3 = rblZJDWDoc3.SelectedValue == "1";
            obj.IsZD4 = rblZJDWDoc4.SelectedValue == "1";
            obj.IsZD_One = rblZD_One.SelectedValue == "1";

            obj.CD1 = txtContDoc1.Text;
            obj.CD2 = txtContDoc2.Text;
            obj.CD3 = txtContDoc3.Text;
            obj.CD4 = txtContDoc4.Text;
            obj.CD5 = txtContDoc5.Text;
            obj.CD6 = txtContDoc6.Text;
            obj.CD7 = txtContDoc7.Text;
            obj.CD8 = txtContDoc8.Text;
            obj.CD9 = txtContDoc9.Text;
            obj.CD_One = txtCD_One.Text;

            obj.PD1 = txtProjDoc1.Text;
            obj.PD2 = txtProjDoc2.Text;
            obj.PD3 = txtProjDoc3.Text;
            obj.PD4 = txtProjDoc4.Text;
            obj.PD5 = txtProjDoc5.Text;
            obj.PD6 = txtProjDoc6.Text;
            obj.PD7 = txtProjDoc7.Text;
            obj.PD8 = txtProjDoc8.Text;
            obj.PD9 = txtProjDoc9.Text;
            obj.PD10 = txtProjDoc10.Text;
            obj.PD_One = txtPD_One.Text;

            obj.ZD1 = txtZJDWDoc1.Text;
            obj.ZD2 = txtZJDWDoc2.Text;
            obj.ZD3 = txtZJDWDoc3.Text;
            obj.ZD4 = txtZJDWDoc4.Text;
            obj.ZD_One = txtZD_One.Text;
            return obj;
        }

	}
}